package com.neu.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.neu.pojo.Fund;
import com.neu.pojo.News;
import com.neu.service.UserService;
import com.neu.utils.ApiUtil;
import com.neu.utils.DateUtil;
import com.neu.utils.HttpClientUtil;
import com.neu.utils.NewsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.*;

@RestController
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private RedisTemplate<String,String> sessionRedisTemplate;
    @Autowired
    private UserService userService;
    @RequestMapping("/getRelatedNews")
    public List<News> getRelatedNews(@RequestParam("session_key") String session_key, @RequestParam("length") int length){
        String openid=sessionRedisTemplate.opsForValue().get(session_key);
        List<Fund> funds=userService.getFavouriteFund(openid);
        HashSet<String> keyWords=new HashSet<>();
        for(Fund f:funds) NewsUtil.getNewsKeyWord(f.getType(),keyWords);
        List<News> result=new ArrayList<>();
        Map<String,String> params=new HashMap<>();
        params.put("key", ApiUtil.API_KEY);
        params.put("num",String.valueOf(1));
        params.put("random",String.valueOf(1));
        Random random=new Random();
        ObjectMapper mapper=new ObjectMapper();
        int relatedNum=length/3;
        int counter=1;
        for(String keyWord:keyWords){
            if(counter>relatedNum) break;
            params.put("word",keyWord);
            String json= HttpClientUtil.doGet("http://api.tianapi.com/caijing/index",params);
            Map<String,Object> resultMap=new HashMap<>();
            try {
                resultMap=mapper.readValue(json, new TypeReference<Map<String, Object>>() {
                });
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            try {
                for(Map<String,String> map:(ArrayList<Map<String,String>>) resultMap.get("newslist")){
                    result.add(new News(map.get("id"), DateUtil.dateFormat.parse(map.get("ctime")),map.get("title"),map.get("source"),map.get("picUrl"),map.get("url")));
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
            counter++;
        }
        String json= HttpClientUtil.doGet("http://api.tianapi.com/caijing/index?key="+ApiUtil.API_KEY+"&num="+String.valueOf(length-relatedNum)+"&rand="+String.valueOf(1));
        Map<String,Object> resultMap=new HashMap<>();
        try {
            resultMap=mapper.readValue(json, new TypeReference<Map<String, Object>>() {
            });
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        try {
            for(Map<String,String> map:(ArrayList<Map<String,String>>) resultMap.get("newslist")){
                result.add(new News(map.get("id"), DateUtil.dateFormat.parse(map.get("ctime")),map.get("title"),map.get("source"),map.get("picUrl"),map.get("url")));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }



}
